home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
demos
/
GL
/
demograph
/
states.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
14KB
|
654 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
#include "stdio.h"
#include "gl.h"
#include "math.h"
#include "device.h"
#include "demograph.h"
#include <sys/time.h>
/* used to flip map (this can be replaced when ui.c is implemented) */
#define TZMAXINC 0.1
#define TXYMAXINC 0.1
#define RMAXINC 50.0
/* number of frames between status updates */
#define WRITE_INC 7
extern MACHREC machrec;
extern WINREC winrec;
extern char *statesbuf;
extern char usage[];
extern int drawtype;
float sry = 200.0;
float srx = -300.0;
float srz = 1.0;
float stz = -3.0;
float sty = -0.25;
float stx = -0.1;
float time0, time1;
static float mytime;
int frames = 0;
Matrix mprojection, mviewing;
void getstates (argc, argv)
int argc;
char *argv[];
{
FILE *file;
long ivar;
int *intptr;
int found = FALSE;
char *statesflag;
char filename[255];
FILE *openfile(char *filename, char *readwrite);
statesflag = "-s";
/* look for states file in parameter list */
for (ivar = 1; ivar < argc; ivar++) {
if (strcmp(statesflag,argv[ivar]) == 0) {
ivar++;
found = TRUE;
break;
}
}
/* check for errors - no data file, no states file */
if (argc < 2 || found && (argc < 4 ||ivar == argc)) {
fprintf(stderr,"%s",usage);
exit(1);
}
/* if not found try default file else try given file */
if (!found)
strcpy(filename,"states.bin");
else
strncpy(filename,argv[ivar],255);
file = openfile(filename,"r");
/* go to end of file, get filesize, then rewind to beginning of file */
fseek(file,0,2);
ivar = ftell(file);
if (ivar == 0) {
fprintf(stderr,"Nothing in states file: %s\n",filename);
exit(1);
}
fseek(file,0,0);
/* make room for states */
statesbuf = (char *)malloc(ivar);
/* blast in states */
fread(statesbuf,sizeof(char),ivar,file);
intptr = (int *)statesbuf;
if (*intptr != CURVERSION) {
fprintf(stderr,"Error: states file %s version is not current\n",
filename);
exit(1);
}
}
typedef float VTX[3];
/* funky states format is as follows:
Each state consists of:
nvolumes number of volumes for this state
npolys number of polygons in firs volume
nvertices number of vertices in top
xyznormal
vtx vtx vtx vtx vtx ...
nvertices number of vertices in side 1
xyznormal
vtx vtx vtx vtx
nvertices number of vertices in side 2
xyznormal
vtx vtx vtx vtx
nvertices number of vertices in side
xyznormal
vtx vtx vtx vtx
...
npolys number of polygons in second volume
nvertices number of vertices in top
xyznormal
vtx vtx vtx vtx vtx ...
nvertices number of vertices in side 1
xyznormal
vtx vtx vtx vtx
nvertices number of vertices in side 2
xyznormal
vtx vtx vtx vtx
nvertices number of vertices in side 3
xyznormal
vtx vtx vtx vtx
nvertices number of vertices in side
xyznormal
vtx vtx vtx vtx
...
Next state goes here
*/
#define BIG_COLOROFFSET 131
#define COLOROFFSET 1
draw_line_map(dataptr)
register float *dataptr;
{
register int p;
register int nv;
register int nvolumes;
register int npolys;
register int nvertices;
register int *intptr;
register VTX *vtxptr;
short r, g, b;
/* we draw the tops here - maybe */
if (drawtype) {
zbuffer(TRUE);
}
if (winrec.fps) {
clock_sec(&time0);
}
mmode(MPROJECTION);
getmatrix(mprojection);
mmode(MVIEWING);
getmatrix(mviewing);
pushviewport();
viewport(winrec.mapminx, winrec.mapmaxx, winrec.mapminy, winrec.mapmaxy);
perspective(300, 1.0, 0.1, 5.0);
/* temporary hardwired scale and rot */
/* pushmatrix(); */
translate(stx,sty,stz);
rotate((int)sry,'y');
rotate((int)srx,'x');
/* inly needed for drawing states */
if (drawtype) {
zclear();
}
setcolor(0,0,0);
clear();
intptr = (int *) statesbuf;
/* bypass version number */
intptr++;
/* 48 states - colors could use some work */
for (r = 127; r < 256; r += 64) { /* 3 times */
for (g = 255; g > 0; g -= 64) { /* 4 times */
for (b = 255; b > 0; b -= 64) { /* 4 times */
/* scale for this state */
pushmatrix();
scale(1.0, 1.0, ((*dataptr) ? *dataptr : MINZSCALE));
/* fprintf(stderr,"%f ",(*dataptr) ? *dataptr:MINZSCALE); */
setcolor(r, g, b);
/* draw each volume */
nvolumes = *intptr++;
for (nv = 0; nv < nvolumes; nv++) {
/* get number of polygons that make up volume */
npolys = *intptr++;
/* get number of vertices for the top */
nvertices = *intptr++;
/* skip over the normal for now */
vtxptr = (VTX *)intptr;
vtxptr++;
/* skip over the top polygon if no zbuffer */
if (drawtype) {
concave(TRUE);
polf(nvertices,vtxptr);
concave(FALSE);
}
vtxptr += nvertices;
intptr = (int *) vtxptr;
/* draw the side polygons - there are always 4
* sides
*/
for (p = 1; p < npolys; p++) {
nvertices = *intptr++;
/* skip over the normal for now */
vtxptr = (VTX *)intptr;
vtxptr++;
poly(nvertices,vtxptr);
vtxptr += nvertices;
intptr = (int *) vtxptr;
}
}
dataptr++;
popmatrix();
}
}
}
/* popmatrix(); */
popviewport();
mmode(MPROJECTION);
loadmatrix(mprojection);
mmode(MVIEWING);
loadmatrix(mviewing);
/* only if we draw the tops */
if (drawtype) {
zbuffer(FALSE);
}
if (winrec.fps) {
clock_sec(&time1);
mytime += (time1 - time0);
frames++;
if (frames == WRITE_INC) {
winrec.lasttime = frames/mytime;
writefps(winrec.lasttime);
frames = 0;
mytime = 0;
}
}
}
draw_solid_map(dataptr)
register float *dataptr;
{
register int p;
register int nv;
register int nvolumes;
register int npolys;
register int nvertices;
register int *intptr;
register float x, y;
register VTX *vtxptr;
short r, g, b;
zbuffer(TRUE);
backface(TRUE);
if (winrec.fps) {
clock_sec(&time0);
}
mmode(MPROJECTION);
getmatrix(mprojection);
mmode(MVIEWING);
getmatrix(mviewing);
pushviewport();
viewport(winrec.mapminx, winrec.mapmaxx, winrec.mapminy, winrec.mapmaxy);
perspective(300, 1.0, 0.1, 5.0);
/* temporary hardwired scale and rot */
/* pushmatrix(); */
translate(stx, sty, stz);
rotate((int) sry, 'y');
rotate((int) srx, 'x');
/* inly needed for drawing states */
zclear();
setcolor(0, 0, 0);
clear();
intptr = (int *) statesbuf;
/* bypass version number */
intptr++;
/* 48 states - colors could use some work */
for (r = 127; r < 256; r += 64) { /* 3 times */
for (g = 255; g > 0; g -= 64) { /* 4 times */
for (b = 255; b > 0; b -= 64) { /* 4 times */
/* fprintf(stderr,"r g b = %d %d %d\n",r,g,b); */
/* scale for this state */
pushmatrix();
scale(1.0, 1.0, ((*dataptr) ? *dataptr : MINZSCALE));
/* fprintf(stderr,"%f ",(*dataptr) ? *dataptr:MINZSCALE); */
/* draw each volume */
nvolumes = *intptr++;
for (nv = 0; nv < nvolumes; nv++) {
setcolor(r, g, b);
/* get number of polygons that make up volume */
npolys = *intptr++;
/* get number of vertices for the top */
nvertices = *intptr++;
/* skip over the normal for now */
vtxptr = (VTX *) intptr;
vtxptr++;
/** intptr = (int *) (vtxptr + 1); **/
/* assume we always draw the top polygon */
/** vtxptr = (VTX *) intptr; **/
concave(TRUE);
polf(nvertices,vtxptr);
concave(FALSE);
/* outline the top on eight bitplane machine */
if (!machrec.RGB) {
color(0);
poly(nvertices,vtxptr);
setcolor(r,g,b);
}
vtxptr += nvertices;
intptr = (int *) vtxptr;
/* draw the side polygons - there are always 4 sides */
for (p = 1; p < npolys; p++) {
nvertices = *intptr++;
/* draw shaded colors */
x = *((float *) intptr);
y = *(((float *) intptr) + 1);
if (x >= 0.0 && y <= 0.0) {
setcolor(
(int)(r*0.5),(int)(g*0.5),(int)(b*0.5));
} else if (x <= 0.0 && y >= 0.0) {
setcolor(
(int)(r*0.35),(int)(g*0.35),(int)(b*0.35));
} else if (x >= 0.0 && y >= 0.0) {
setcolor(
(int)(r*0.2),(int)(g*0.2),(int)(b*0.2));
} else {
setcolor(
(int)(r*0.7),(int)(g*0.7),(int)(b*0.7));
}
/* skip over the normal for now */
vtxptr = (VTX *) intptr;
vtxptr++;
/* draw the side */
polf(nvertices,vtxptr);
vtxptr += nvertices;
intptr = (int *) vtxptr;
}
}
dataptr++;
popmatrix();
}
}
}
/* popmatrix(); */
popviewport();
mmode(MPROJECTION);
loadmatrix(mprojection);
mmode(MVIEWING);
loadmatrix(mviewing);
zbuffer(FALSE);
backface(FALSE);
if (winrec.fps) {
clock_sec(&time1);
mytime += (time1 - time0);
frames++;
if (frames == WRITE_INC) {
winrec.lasttime = frames/mytime;
writefps(winrec.lasttime);
frames = 0;
mytime = 0;
}
}
}
draw_lighted_map(dataptr)
register float *dataptr;
{
register int p;
register int nv;
register int i;
register int nvolumes;
register int npolys;
register int nvertices;
register int *intptr;
register VTX *vtxptr;
register VTX *normal;
short r, g, b;
if (winrec.fps) {
clock_sec(&time0);
}
shademodel(GOURAUD);
zbuffer(TRUE);
backface(TRUE);
mmode(MPROJECTION);
getmatrix(mprojection);
mmode(MVIEWING);
getmatrix(mviewing);
pushviewport();
viewport(winrec.mapminx, winrec.mapmaxx, winrec.mapminy, winrec.mapmaxy);
perspective(300, 1.0, 0.1, 5.0);
lmbind(LIGHT0, 1);
/* temporary hardwired scale and rot */
/* pushmatrix(); */
translate(stx, sty, stz);
rotate((int) sry, 'y');
rotate((int) srx, 'x');
lmcolor(LMC_AD);
/* inly needed for drawing states */
zclear();
setcolor(0, 0, 0);
clear();
intptr = (int *) statesbuf;
/* bypass version number */
intptr++;
/* 48 states - colors could use some work */
for (r = 127; r < 256; r += 64) { /* 3 times */
for (g = 255; g > 0; g -= 64) { /* 4 times */
for (b = 255; b > 0; b -= 64) { /* 4 times */
RGBcolor(r, g, b);
/* scale for this state */
pushmatrix();
scale(1.0, 1.0, ((*dataptr) ? *dataptr : MINZSCALE));
/* fprintf(stderr,"%f ",(*dataptr) ? *dataptr:MINZSCALE); */
/* draw each volume */
nvolumes = *intptr++;
for (nv = 0; nv < nvolumes; nv++) {
/* get number of polygons that make up volume */
npolys = *intptr++;
/* get number of vertices for the top */
nvertices = *intptr++;
/* get the normal */
vtxptr = (VTX *) intptr;
normal = vtxptr++;
/* assume we always draw the top polygon */
concave(TRUE);
bgnpolygon();
for (i = nvertices; i > 0; i--) {
n3f(normal);
v3f(vtxptr++);
}
endpolygon();
concave(FALSE);
intptr = (int *) vtxptr;
/* draw the side polygons - there are always 4 sides */
for (p = 1; p < npolys; p++) {
nvertices = *intptr++;
/* get the normal */
vtxptr = (VTX *) intptr;
normal = vtxptr++;
/* draw this side */
bgnpolygon();
for (i = 0; i < nvertices; i++) {
n3f(normal);
v3f(vtxptr++);
}
endpolygon();
intptr = (int *) vtxptr;
}
}
dataptr++;
popmatrix();
}
}
}
/* popmatrix(); */
popviewport();
mmode(MPROJECTION);
loadmatrix(mprojection);
mmode(MVIEWING);
loadmatrix(mviewing);
lmcolor(LMC_COLOR);
zbuffer(FALSE);
backface(FALSE);
shademodel(FLAT);
if (winrec.fps) {
clock_sec(&time1);
mytime += (time1 - time0);
frames++;
if (frames == WRITE_INC) {
winrec.lasttime = frames/mytime;
writefps(winrec.lasttime);
frames = 0;
mytime = 0;
}
}
}
flipmap(mode)
int mode;
{
float tzscaler, txyscaler, rscaler;
int x, y;
int cx, cy;
int orgx, orgy;
int dev = 0;
short val = 0;
cx = (int) ((winrec.mapminx + winrec.mapmaxx) / 2.0 + 0.5);
cy = (int) ((winrec.mapminy + winrec.mapmaxy) / 2.0 + 0.5);
/* fprintf(stderr,"mminx%d mmaxx%d mminy%d mmaxy%d cy%d cx%d\n",
winrec.mapminx,winrec.mapmaxx,winrec.mapminy,winrec.mapmaxy,cy,cx);
*/
orgx = winrec.orgx;
orgy = winrec.orgy;
/* fprintf(stderr,"orgx%d orgy%d\n",orgx,orgy); */
tzscaler = TZMAXINC / cx;
txyscaler = TXYMAXINC / cx;
rscaler = RMAXINC / cx;
do {
x = getvaluator(MOUSEX) - orgx;
y = getvaluator(MOUSEY) - orgy;
/* fprintf(stderr,"x%d y%d\n",x,y); */
if (getview(x,y) == MAPPORT) {
switch (mode) {
case SPIN:
sry += (x - cx) * rscaler;
srx += -((y - cy) * rscaler);
break;
case TRANSXY:
stx += (x - cx) * txyscaler;
sty += (y - cy) * txyscaler;
case TRANSZ:
stz += (x - cx) * tzscaler;
}
drawaction();
}
} while (!(qtest() &&
((dev = qread(&val)) == LEFTMOUSE && mode == TRANSXY) ||
(dev == MIDDLEMOUSE && mode == SPIN) &&
val == UP));
qreset();
}
clock_sec(s)
float *s;
{
float tmp1,tmp2;
long err;
struct timeval tp;
struct timezone tzp;
err = gettimeofday(&tp,&tzp);
tmp1 = (float)(tp.tv_usec)/1000000.0;
tmp2 = tp.tv_sec%10000;
*s = tmp1+tmp2;
return;
}